package com.example.demo01.controller;

import com.example.demo01.eneity.YzfUser;
import com.example.demo01.service.YzfUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class YzfUserController {

    // 注入YzfUserService
    @Autowired
    private YzfUserService yzfUserService;

    // 插入用户
    @PostMapping("/insert")
    public void insertUser(@RequestBody YzfUser user) {
        yzfUserService.insertUser(user);
    }

    // 根据用户ID删除用户
    @DeleteMapping("/delete/{userId}")
    public void deleteUser(@PathVariable int userId) {
        yzfUserService.deleteUserById(userId);
    }

    // 根据用户ID更新用户
    @PutMapping("/update/{userId}")
    public void updateUser(@PathVariable int userId, @RequestBody YzfUser updatedUser) {
        // 设置要更新的用户的ID
        updatedUser.setUserID(userId);
        yzfUserService.updateUser(updatedUser);
    }

    // 根据用户名搜索用户
    @GetMapping("/search")
    public List<YzfUser> searchUsersByUsername(@RequestParam("username") String username) {
        return yzfUserService.findUsersByUsername(username);
    }


}